package com.songjiasong.activity;

import java.util.Calendar;
import java.util.GregorianCalendar;

import com.songjiasong.R;
import com.songjiasong.db.NoteBookSchema;
import com.songjiasong.db.NoteBookVo;
import com.songjiasong.db.dao.NoteBookDao;
import com.songjiasong.util.DateUtil;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class DetailActivity extends Activity {

	private Button saveBtn;
	private Button backBtn;
	private TextView titleText;
	private TextView contentText;
	private DatePickerDialog datePickerDialog;
	private NoteBookVo vo;
	private NoteBookDao dao;
	private CharSequence method;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.detail);
		
		dao = new NoteBookDao(this);
		vo = new NoteBookVo();
		
		GregorianCalendar calendar = new GregorianCalendar();
		saveBtn = (Button)this.findViewById(R.id.SaveBtn);
		backBtn = (Button)this.findViewById(R.id.BackBtn);
		titleText = (TextView)this.findViewById(R.id.TitleText);
		contentText = (TextView)this.findViewById(R.id.ContentText);
		datePickerDialog = new DatePickerDialog(this,dateSetListener
				,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
		
		Bundle extras = this.getIntent().getExtras();
		if(extras != null){
			method = extras.getCharSequence("Method");
			if(method.equals("Edit")){
				NoteBookVo example = new NoteBookVo();
				backBtn.setText(R.string.Delete);
				example.setDate(extras.getCharSequence(NoteBookSchema.DATE));
				CharSequence title = extras.getCharSequence(NoteBookSchema.TITLE);
				example.setTitle(title);
				vo = dao.findByExample(example);
				contentText.setText(vo.getContent());
				titleText.setText(vo.getTitle());
				String[] date = vo.getDate().toString().split("/");
				datePickerDialog = new DatePickerDialog(this,dateSetListener
						,Integer.parseInt(date[0]),Integer.parseInt(date[1])-1,Integer.parseInt(date[2]));
			}
		}
		
		saveBtn.setOnClickListener(onSave);
		backBtn.setOnClickListener(onBack);
	}
	
	private void doAdd(CharSequence date){
		
		Intent intent = new Intent();
		vo.setTitle(titleText.getText());
		vo.setContent(contentText.getText());
		vo.setDate(date);
		dao.addNote(vo);
		intent.putExtra("Btn", "Save");
		setResult(RESULT_OK,intent);
		finish();
	}
	
	private void doEdit(CharSequence date){
		Intent intent = new Intent();
		vo.setTitle(titleText.getText());
		vo.setContent(contentText.getText());
		vo.setDate(date);
		dao.updateNote(vo);
		intent.putExtra("Btn", "Save");
		setResult(RESULT_OK,intent);
		finish();
	}
	
	OnDateSetListener dateSetListener = new OnDateSetListener(){
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			CharSequence date = DateUtil.getDate(year, monthOfYear, dayOfMonth);			
			if(method.equals("Add")){
				doAdd(date);
			}else if(method.equals("Edit")){
				doEdit(date);
			}
		}
		
	};
	
	OnClickListener onSave = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			datePickerDialog.show();			
		}
	};
	
	OnClickListener onBack = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			if(method.equals("Add")){
				intent.putExtra("Btn", "Back");
			}else if(method.equals("Edit")){
				intent.putExtra("Btn", "Del");
				dao.deleteNote(vo);
			}
			
			setResult(RESULT_OK,intent);
			finish();	
		}
	};

}
